package de.tum.icuproject;

import java.util.Locale;

import de.tum.icuproject.model.Player;
import de.tum.icuproject.utils.AppUtils;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;

/**
 * The MainActivity provides the fragments.
 * Functionality can be found there.
 * 
 */
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

	SectionsPagerAdapter mSectionsPagerAdapter;

	ViewPager mViewPager;

	private Player currentPlayer;

	public Player getCurrentPlayer() {
		return currentPlayer;
	}

	public void setCurrentPlayer(Player player) {
		this.currentPlayer = player;
	}

	// Receives data updates from the MDCClient thread.
	@SuppressLint("HandlerLeak")
	private Handler clientHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			Bundle bundle = msg.getData();
			double heartRate = bundle.getDouble(AppUtils.HEART_RATE);
			double resp = bundle.getDouble(AppUtils.RESPIRATION);
			double speed = bundle.getDouble(AppUtils.SPEED);
			double load = bundle.getDouble(AppUtils.LOAD);
			Log.d(AppUtils.HEART_RATE, "Handler received: " + heartRate);
			Log.d(AppUtils.RESPIRATION, "Handler received: " + resp);
			Log.d(AppUtils.SPEED, "Handler received: " + speed);
			Log.d(AppUtils.LOAD, "Handler received: " + load);

			MonitorFragment.updateGraphViews(heartRate, resp, speed, load);
		}

	};

	public Handler getClientHandler() {
		return this.clientHandler;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Set up the action bar.
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		// Create the adapter that will return a fragment for each of the three primary sections of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

		// When swiping between different sections, select the corresponding tab.
		mViewPager
		.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
			}
		});

		// For each of the sections in the app, add a tab to the action bar.
		for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
			// Create a tab with text corresponding to the page title defined by
			// the adapter. Also specify this Activity object, which implements
			// the TabListener interface, as the callback (listener) for when
			// this tab is selected.
			actionBar.addTab(actionBar.newTab()
					.setText(mSectionsPagerAdapter.getPageTitle(i))
					.setTabListener(this));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onTabSelected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
		// When the given tab is selected, switch to the corresponding page in
		// the ViewPager.
		mViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
	}

	@Override
	public void onTabReselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
	}

	public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
		Fragment playerFragment, parameterFragment, monitorFragment;

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
			playerFragment = new PlayerFragment();
			((PlayerFragment) playerFragment).setSectionsPagerAdapter(this);
			parameterFragment = new ParameterFragment();
			monitorFragment = new MonitorFragment();
		}

		@Override
		public Fragment getItem(int position) {
			// Return the fragment according to the position:
			switch (position) {
			case 0:
				return playerFragment;
			case 1:
				return parameterFragment;
			case 2:
				return monitorFragment;
			default:
				return null;
			}
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return getString(R.string.title_player_fragment).toUpperCase(l);
			case 1:
				return getString(R.string.title_paramenters_fragment)
						.toUpperCase(l);
			case 2:
				return getString(R.string.title_monitor_fragment)
						.toUpperCase(l);
			}
			return null;
		}

		public void updatePlayerFragment(Fragment newFragment) {
			playerFragment = newFragment;
			this.notifyDataSetChanged();
		}
	}

}
